﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SEPD.Template.Revit1
{
    /// <summary>
    /// TemplateView.xaml 的交互逻辑
    /// </summary>
    public partial class TemplateView : Window
    {
        public TemplateView()
        {
            InitializeComponent();
        }

    }

    /// <summary>
    /// DataGrid内自定义多选
    /// </summary>
    public class CustomDataGrid : DataGrid
    {
        public CustomDataGrid()
        {
            this.SelectionChanged += CustomDataGrid_SelectionChanged;
        }

        void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.SelectedItemsList = this.SelectedItems;
        }
        #region SelectedItemsList

        public IList SelectedItemsList
        {
            get { return (IList)GetValue(SelectedItemsListProperty); }
            set { SetValue(SelectedItemsListProperty, value); }
        }

        public static readonly DependencyProperty SelectedItemsListProperty =
                DependencyProperty.Register("SelectedItemsList", typeof(IList),
                    typeof(CustomDataGrid), new PropertyMetadata(null));

        #endregion
    }

    public class CustomeSelectionItems
    {
        public static IList GetSelectedItems(DependencyObject obj)
        {
            return (IList)obj.GetValue(SelectedItemsProperty);
        }

        public static void SetSelectedItems(DependencyObject obj, IList value)
        {
            obj.SetValue(SelectedItemsProperty, value);
        }

        //Using a DependencyProperty as the backing store for SelectedItems.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedItemsProperty =
            DependencyProperty.RegisterAttached("SelectedItems",
                typeof(IList),
                typeof(CustomeSelectionItems),
                new PropertyMetadata(OnSelectedItemsChanged));

        static public void OnSelectedItemsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            try
            {
                var listBox = d as DataGrid;
                if ((listBox != null) && (listBox.SelectionMode == DataGridSelectionMode.Extended))
                {
                    if (e.OldValue != null)
                    {
                        listBox.SelectionChanged -= OnlistBoxSelectionChanged;
                    }
                    IList collection = e.NewValue as IList;
                    listBox.SelectedItems.Clear();
                    if (collection != null)
                    {
                        foreach (var item in collection)
                        {
                            listBox.SelectedItems.Add(item);
                        }
                        listBox.SelectionChanged += OnlistBoxSelectionChanged;
                    }
                }
            }
            catch (Exception)
            {
                ;
            }
        }

        static void OnlistBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                IList dataSource = GetSelectedItems(sender as DependencyObject);
                //添加用户选中的当前项.
                foreach (var item in e.AddedItems)
                {
                    dataSource.Add(item);
                }
                //删除用户取消选中的当前项
                foreach (var item in e.RemovedItems)
                {
                    dataSource.Remove(item);
                }
            }
            catch (Exception)
            { }
        }
    }





}
